import AnimatorBase, {
  AnimationPlayer,
} from "db://app-game/libs/animator/core/AnimatorBase";

class FrameEventData {
  public callback!: Function;
  public target: any;
}

/** 模型动作自定义事件逻辑 */
export class AnimationEventHandler<T> implements AnimationPlayer {
  private frameEvents: Map<string, FrameEventData> = new Map();
  private finishedEvents: Map<string, FrameEventData> = new Map();

  public playAnimation(animName: string, loop: boolean): void {}
  constructor(playAnimation) {
    this.playAnimation = playAnimation;
  }

  public addFrameEvent(type: string, callback: Function, target: any) {
    var data = new FrameEventData();
    data.callback = callback;
    data.target = target;
    this.frameEvents.set(type, data);
  }

  public addFinishedEvent(type: string, callback: Function, target: any) {
    var data = new FrameEventData();
    data.callback = callback;
    data.target = target;
    this.finishedEvents.set(type, data);
  }

  onFrameEventCallback<T>(type: string, target: AnimatorBase, param: T): void {
    var data = this.frameEvents.get(type);
    if (data) data.callback.call(data.target, type, target, param);
  }

  onFinishedCallback(target: AnimatorBase): void {
    console.log("onFrameEventCallback", target);
    var data = this.finishedEvents.get(target.curStateName);
    if (data) data.callback.call(data.target, target.curStateName, target);
  }

  scaleTime(scale: number): void {}
}
